package com.you.pattern.sigleton.lazy;

/**
 * 通过静态内部类创建单例
 */
public class LazyInnerClassSingleton {

    public static LazyInnerClassSingleton lazyInnerClassSingleton;

    /**
     * 抛出异常，防止通过发射创建对象
     */
    private LazyInnerClassSingleton() {
        if (LazyInnerClassSigletonHolder.LAZY_INNER_CLASS_SINGLETON != null) {
            throw new RuntimeException("不允许创建多个对象!");
        }
    }

    //每一个关键字都不是多余的
    // static 是为了使单例的空间共享
    // final保证这个方法不会被重写，重载
    public static final LazyInnerClassSingleton getInstance() {
        return LazyInnerClassSigletonHolder.LAZY_INNER_CLASS_SINGLETON;
    }

    //默认不加载
    private static class LazyInnerClassSigletonHolder {
        private static final LazyInnerClassSingleton LAZY_INNER_CLASS_SINGLETON = new LazyInnerClassSingleton();
    }

    public static void main(String[] args) {
        System.out.println(LazyInnerClassSingleton.getInstance());
    }
}
